import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

class NameDialog extends JDialog implements ActionListener {
	private JButton okButton = null;
	private JButton cancelButton = null;
	private JTextField nameField = null;
	String name = null, mail=null;
	private JTextField email = null;
	private ArrayList<Sitz> platz;
	private Vorfuehrung vfr;
	private Database db;
	private User user;

	public NameDialog(ArrayList<Sitz> plaetze, Vorfuehrung vfr, Database db, User user) {
		// super("Name Dialog", true);

		this.db = db;
		this.user=user;
        platz=new ArrayList<Sitz>();
        for(int i=0;i<plaetze.size();i++)
        	platz.add(plaetze.get(i));
		this.vfr=vfr;
		System.out.println(plaetze.get(0));
		Container cp = getContentPane();
		cp.setLayout(new BorderLayout());

		cp.add("North", new JLabel("", SwingConstants.LEFT));

		JPanel inputPanel = new JPanel();
		inputPanel.setLayout(new GridLayout(3, 2));
		inputPanel
				.add(new JLabel(
						"Bitte geben Sie Ihren Namen und Ihre E-Mail-Adresse ein:"));
		inputPanel.add(new JLabel(""));
		nameField = new JTextField(20);
		email = new JTextField();
		inputPanel.add(new JLabel("Name"));
		inputPanel.add(nameField);
		inputPanel.add(new JLabel("E-Mail"));
		inputPanel.add(email);

		cp.add("Center", inputPanel);

		JPanel buttonPanel = new JPanel();
		cp.add("South", buttonPanel);

		okButton = new JButton("Reservierung senden");
		okButton.addActionListener(this);
		buttonPanel.add(okButton);

		cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(this);
		buttonPanel.add(cancelButton);

		
		
		pack();
		setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == okButton) {
			name = nameField.getText();
			mail = email.getText();
			dispose();
			try {
				Reservierungswunsch rw = new Reservierungswunsch(db, vfr, platz, name, mail);
			} catch (IOException e1) {
				e1.printStackTrace();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			try {
				new Kinoverwaltung(db, user);
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		if (e.getSource() == cancelButton) {
			name = null;
			dispose();
			try {
				new Kinoverwaltung(db, user);
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}

	public String getName() {
		return name;
	}
	public String getMail() {
		return mail;
	}

}


